<?php

namespace app\admin\controller\classroom;

use app\common\controller\Backend;
use think\Db;

/**
 * 提交试卷
 *
 * @icon fa fa-circle-o
 */
class TestLog extends Backend
{

    /**
     * TestLog模型对象
     * @var \app\admin\model\classroom\TestLog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\classroom\TestLog;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("isPassList", $this->model->getIsPassList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index($test_id = null)
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->where("test_id",$test_id)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['nickname']);
            }

            $total_num = Db::name('classroom_test_log')->where('test_id',$test_id)->count();
            $no_num = Db::name('classroom_test_log')->where('test_id',$test_id)->where('status',0)->count();
            $yes_num = Db::name('classroom_test_log')->where('test_id',$test_id)->where('status',1)->count();

            $result = array("total" => $total_num, "rows" => $list->items(), "extend" => ["no_num"=>$no_num,"yes_num"=>$yes_num]);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 批改
     */
    public function check()
    {
        $id = $this->request->get('id');
        $test_log = Db::name('classroom_test_log')->where("id",$id)->find();
        $content = json_decode($test_log['content'],true);
        if($this->request->isPost()){
            if($test_log['status'] == 1){
                $this->error('请不要重新批阅');
            }
            $score_arr = $this->request->post('score_arr/a');
            $score_num = $this->request->post('score_num/a');

            if(!$score_arr) {
                $this->error('请输入完整');
            }
            $score_count = 0;
            foreach ($score_arr as $k=>$v) {
                foreach ($v as $k2=>$v2) {
                    $content[$k-1]['question'][$k2-1]['ask_score'] = $v2;
                    $score_count += $v2;
                    if($v2 > $score_num[$k][$k2]){
                        $this->error('评分不能超过设定分数');
                    }
                }

            }
            $total_score = bcadd($test_log['score'],$score_count);
            $is_pass = $total_score >= $test_log['pass_score'] ? 1 : 0;
            $re = Db::name('classroom_test_log')->where("id",$id)->update(['score'=>$total_score,'is_pass'=>$is_pass,'status'=>1,'content'=>json_encode($content)]);
            if($re !== false){
                $assign_status = $is_pass == 1 ? '完成' : '未及格';
                Db::name("classroom_test_assign")->where("test_id",$test_log['test_id'])->where("user_id",$test_log['user_id'])->update(['status'=>$assign_status]);

                $this->success('批阅成功');
            }
            $this->error('批阅失败');
        }
        $test_id = $test_log['test_id'];
        $test_data = Db::name('classroom_test')->where('id',$test_id)->find();

        foreach ($content as $k=>$v) {
            if($v['value'] == '问答题'){
                foreach ($v['question'] as $k2=>$v2) {
                    $content[$k]['question'][$k2]['reference'] = Db::name("classroom_question")->where("id",$v2['id'])->value("answer");
                }
            }
        }
        // dump($content);
        $this->view->assign('test_data',$test_data);
        $this->view->assign('question',$content);
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    public function detail($ids = null)
    {
        $test_log = Db::name('classroom_test_log')->alias("a")->join("user b","a.user_id = b.id","LEFT")
            ->field("a.*,b.nickname")
            ->where("a.id",$ids)->find();
        $result = json_decode($test_log['content'],true);
        $test_id = $test_log['test_id'];
        $test_data = Db::name('classroom_test')->where('id',$test_id)->find();

        $this->view->assign('test_data',$test_data);
        $this->view->assign('question',$result);
        $this->view->assign('nickname',$test_log['nickname']);
        $this->view->assign('test_score',$test_log['score']);
        return $this->view->fetch();
    }


}











